Notes

require v library - library provides an error message require provides a true false statement

…/ lets you go up a file path ### Load Libraries

library(raster)
## Loading required package: sp
library(rgdal)
## rgdal: version: 1.1-10, (SVN revision 622)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 1.11.4, released 2016/01/25
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/proj
##  Linking to sp version: 1.2-3

Import Canopy Height Model

chm <- raster("../NEONdata/D17-California/TEAK/2013/lidar/TEAK_lidarCHM.tif")

#this plots using the raster package
plot(chm)

# these are just pixels ans will stretc the space
image(chm)

Deal with 0 values

hist(chm)
## Warning in .hist1(x, maxpixels = maxpixels, main = main, plot = plot, ...):
## 32% of the raster cells were used. 100000 values used.

chm[chm==0] <- NA

hist(chm,
     xlab = "Tree Height (m)")

Import Aspect Data

aspect <- raster("../NEONdata/D17-California/TEAK/2013/lidar/TEAK_lidarAspect.tif")

plot(aspect,
     main = "Aspect for Teakettle Field Site")

Creat Aspect Classifcation Matrix

# north will be class 1 (0-45 degrees as well as 315-360)
# south will be class 2 (135-225 degrees)
class.m <- c(0, 45, 1,
             45, 135, NA,
             135, 225, 2,
             225, 315, NA,
             315, 360, 1)

# redefine the array above to a matrix
rcl.m <- matrix(class.m,
          ncol = 3,
          byrow = TRUE)

rcl.m
##      [,1] [,2] [,3]
## [1,]    0   45    1
## [2,]   45  135   NA
## [3,]  135  225    2
## [4,]  225  315   NA
## [5,]  315  360    1

Reclassify Raster

asp.ns <- reclassify(aspect,
                     rcl.m)

plot(asp.ns,
     main = "North and South Facing Slopes")

Export Geotiff

writeRaster(asp.ns,
            file = "../outputs/TEAK/TEAKnsAspect2.tif",
            options = "COMPRESS=LZW",
            NAflag = -9999)

Mask Raster based on pixel values

asp.ns
## class       : RasterLayer 
## dimensions  : 577, 543, 313311  (nrow, ncol, ncell)
## resolution  : 1, 1  (x, y)
## extent      : 325963, 326506, 4102905, 4103482  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=11 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
## data source : in memory
## names       : layer 
## values      : 0, 2  (min, max)
ndvi <- raster("../NEONdata/D17-California/TEAK/2013/spectrometer/veg_index/TEAK_NDVI.tif")

plot(ndvi,
     main = "NDVI for Teakettle")

# mask data

nFacing.ndvi <- mask(ndvi,asp.ns)

plot(nFacing.ndvi)